package com.gjt.dlmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author gjt
 * @version 1.0
 * @date 2021/9/4 19:49
 */
@Configuration
public class DlmallConfiguration {
    /**
     * 配置mvc 解决跨域问题
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration config = new CorsConfiguration();
        // Possibly...
        // config.applyPermitDefaultValues()
        config.setAllowCredentials(true);//可以携带cookie信息
        config.addAllowedOrigin("*");//允许所有的请求地址访问
        config.addAllowedHeader("*");//允许携带所有的请求头访问
        config.addAllowedMethod("*");//允许所有的请求方式 get post...

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }
}
